package com.photoeditor.demo.util;

import android.content.Context;
import android.graphics.Point;
import android.util.DisplayMetrics;

import com.common.base.ComponentContext;

/**
 * Created by mutour on 2017/5/10.
 */

public final class DisplayUtils {

    public static float SCREEN_DENSITY = 0;
    public static int SCREEN_WIDTH = 0;
    public static int SCREEN_HEIGHT = 0;

    private static final float ADJUSTVALUE_FOR_DIP_PX_TRANSFORM = 0.5f;

    static{
        DisplayMetrics dm = ComponentContext.getContext().getResources().getDisplayMetrics();
        SCREEN_WIDTH = dm.widthPixels;
        SCREEN_HEIGHT = dm.heightPixels;
        SCREEN_DENSITY = dm.density;
    }
    public static int dp2px(float dpValue) {
        return (int) (dpValue * SCREEN_DENSITY + ADJUSTVALUE_FOR_DIP_PX_TRANSFORM);
    }

    public static int px2dp(float pxValue) {
        return (int) (pxValue / SCREEN_DENSITY + ADJUSTVALUE_FOR_DIP_PX_TRANSFORM);
    }


    public static int pxtosp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }


    public static Point getScreenMetrics(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        return new Point(screenWidth, screenHeight);

    }

}
